/**
 * JavaScript
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-19
 * @package railbrowser
 */


/* Get the inventory via AJAX call. Set inventory innerHTML and start dojo.parser for inventory.
 *
 */
function inventory_refresh () {
	ajax_call( 'inventory', {load: function( ajax_response ) {
		document.getElementById('inventory').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('inventory') );
	}} );
}


/* Send rights_form formular via AJAX call.
 *
 */
function rights_update () {
	ajax_call_post( 'rights_form', 'rights_update', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Beim speichern der Rechte ist ein Fehler aufgetreten:\n" + ajax_response.exceptions[0] );
			return
		}

		dijit.byId('rights_rekursiv').setChecked( false );
	}} );
}


/* ------------------------------------ attributes ------------------------------------ */

/* Send attribute_add formular via AJAX call. Refresh attributes and hide add_attribute Dialog.
 *
 */
function attribute_add () {
	ajax_call_post( 'add_attribute_form', 'attribute_add', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Das Attribute konnte nicht hinzugefügt werden:\n" + ajax_response.exceptions[0] );
			return
		}

		attributes_refresh();
		dijit.byId('add_attribute').hide();
	}} );
}


/* Send attribute to remove via AJAX call. Refresh attributes.
 *
 * @param string attribute
 */
function attribute_delete ( attribute ) {
	if( window.confirm('Das Attribute \''+attribute+'\' wird gelöscht.') )
		ajax_call( 'attribute_delete&attribute='+attribute, {load: function( ajax_response ) {
			// on error response
			if( ajax_response.type == 'ajax_error_response' ){
				alert("Das Attribite '"+attribute+"' konnte nicht gelöscht werden:\n" + ajax_response.exceptions[0] );
				return
			}

			attributes_refresh();
		}} );
}


/* Get all attributes via AJAX call. Set attributes_form innerHTML and start dojo.parser for attributes_form.
 *
 */
function attributes_refresh () {
	ajax_call( 'attributes', {load: function( ajax_response ) {
		document.getElementById('attributes_form').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('attributes_form') );
	}} );
}


/* Send attributes_form formular via AJAX call.
 *
 */
function attributes_update () {
	ajax_call_post( 'attributes_form', 'attributes_update', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Das Speichern von mindestens einem Attribut ist fehlgeschlagen:\n" + ajax_response.exceptions[0] );
			return
		}
	}} );
}


/* -------------------------------------- object -------------------------------------- */

/* Send object_add formular via AJAX call. Refresh inventory and hide add_object Dialog.
 *
 */
function object_add () {
	ajax_call_post( 'add_object_form', 'object_add', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Das Objekt konnte nicht hinzugefügt werden:\n" + ajax_response.exceptions[0] );
			return
		}

		inventory_refresh();
		dijit.byId('add_object').hide();
	}} );
}


/* Copy to clipboard the rail_object with rail_id. If rail_id isn't set, copy current object.
 *
 * @param int rail_id (optional)
 */
function object_copy ( rail_id ) {
	ajax_call( 'object_copy' + (rail_id ? '&obj='+ rail_id : ''), {load: function( ajax_response ) {}} );
}


/* Cut(copy to clipboard) the rail_object with rail_id. If rail_id isn't set, cut current object.
 *
 * @param int rail_id (optional)
 */
function object_cut ( rail_id ) {
	ajax_call( 'object_copy&cut' + (rail_id ? '&obj='+ rail_id : ''), {load: function( ajax_response ) {}} );
}


/* Delete the rail_object with rail_id. If rail_id isn't set, delete current object.
 *
 * @param int rail_id (optional)
 */
function object_delete ( rail_id ) {
	if( window.confirm('Dieses Objekt wird gelöscht.') )
		ajax_call( 'object_delete' + (rail_id ? '&obj='+ rail_id : ''), {load: function( ajax_response ) {
			// on error response
			if( ajax_response.type == 'ajax_error_response' ){
				alert("Das Objekt konnte nicht gelöscht werden:\n" + ajax_response.exceptions[0] );
				return
			}

			// load invironment if deleted current object otherwise refresh inventory
			if( !rail_id )
				object_load( ajax_response.environment );
			else
				inventory_refresh();
		}} );
}


function object_locate () {
	ajax_call_post( 'locate_form', 'object_locate', {load: function( ajax_response ) {
		// on error response
		if( ajax_response.type == 'ajax_error_response' ){
			alert("Das Objekt existiert nicht!");
			return
		}

		object_load( ajax_response.id );
	}} );
}


/* Load the rail_object with rail_id to railbrowser via AJAX call. Set info table, rights and refresh all.
 *
 * @param int rail_id
 */
function object_load ( rail_id ) {
	ajax_call( 'object&object='+rail_id, {load: function( ajax_response ) {
		dijit.byId('rail_id').setValue( ajax_response.id );
		dijit.byId('rail_path').setValue( ajax_response.path );

		// set obj_info
		document.getElementById('obj_info').innerHTML = 'Name: <b>'+ajax_response.name+'</b><br />rail_id: '+ajax_response.id+'<br />Type: '+ajax_response.type+'<br />Mode: '+ajax_response.mode+'<br />Erstellt: '+ajax_response.t_create+'<br />Geändert: '+ajax_response.t_lastwrite;
		if( ajax_response.is_readable )
			document.getElementById('obj_icon').className = ajax_response.type;
		else
			document.getElementById('obj_icon').className = ajax_response.type+' not_readable';

		// set rights
		var store = new dojo.data.ItemFileReadStore({url:"call.php?func=users"});
		dijit.byId('rights_user').store = store;
		dijit.byId('rights_user').setValue( ajax_response.user );
		var store = new dojo.data.ItemFileReadStore({url:"call.php?func=groups"});
		dijit.byId('rights_group').store = store;
		dijit.byId('rights_group').setValue( ajax_response.group );
		dijit.byId('rights_user_mode').setValue( ajax_response.mode.substring(0,1) );
		dijit.byId('rights_group_mode').setValue( ajax_response.mode.substring(1,2) );
		dijit.byId('right_other').setValue( ajax_response.mode.substring(2,3) );
		dijit.byId('rights_user').setDisabled( !ajax_response.is_root );
		dijit.byId('rights_group').setDisabled( !ajax_response.is_user && !ajax_response.is_root );
		dijit.byId('rights_user_mode').setDisabled( !ajax_response.is_user && !ajax_response.is_root );
		dijit.byId('rights_group_mode').setDisabled( !ajax_response.is_user && !ajax_response.is_root );
		dijit.byId('right_other').setDisabled( !ajax_response.is_user && !ajax_response.is_root );
		dijit.byId('rights_save').setDisabled( !ajax_response.is_user && !ajax_response.is_root );
		dijit.byId('rights_rekursiv').setDisabled( !ajax_response.is_user && !ajax_response.is_root );

		// disable/enable menu items
		dijit.byId('menu_addAttribute').setDisabled( !ajax_response.is_writeable );
		dijit.byId('menu_addObject').setDisabled( !ajax_response.is_writeable );
		dijit.byId('menu_copy').setDisabled( !ajax_response.is_readable );
		dijit.byId('menu_cut').setDisabled( !ajax_response.is_writeable );
		dijit.byId('menu_paste').setDisabled( !ajax_response.is_writeable );
		dijit.byId('menu_delete').setDisabled( !ajax_response.is_writeable );

		// refresh
		attributes_refresh();
		relations_refresh();
		inventory_refresh();
	}} );
}


/* Include the rail_object from clipboard to the inventory of the rail_object with rail_id. If rail_id isn't set,
 * include to current object's inventory.
 *
 * @param int rail_id (optional)
 */
function object_paste ( rail_id ) {
	ajax_call( 'object_paste' + (rail_id ? '&obj='+ rail_id : ''), {load: function( ajax_response ) {
		inventory_refresh();
	}} );
}


/* ------------------------------------ relations ------------------------------------- */

/* Get all relations via AJAX call. Set relations_pane innerHTML and start dojo.parser for relations_pane.
 *
 */
function relations_delete ( rail_id, type ) {
	if( window.confirm('Die Beziehung wird gelöscht.') )
		ajax_call( 'relation_delete&obj='+rail_id+'&type='+type, {load: function( ajax_response ) {
			// on error response
			if( ajax_response.type == 'ajax_error_response' ){
				alert("Das Attribite '"+attribute+"' konnte nicht gelöscht werden:\n" + ajax_response.exceptions[0]);
				return
			}

			relations_refresh();
		}} );
}


/* Get all relations via AJAX call. Set relations_pane innerHTML and start dojo.parser for relations_pane.
 *
 */
function relations_refresh () {
	ajax_call( 'relations', {load: function( ajax_response ) {
		document.getElementById('relations_pane').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('relations_pane') );
	}} );
}